package Demo2;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-05-22
 * Time: 18:45
 */
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}
public class Test {
    public static void mySort(Comparable arr[]){
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j].compareTo(arr[j + 1]) > 0){
                    Comparable tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student[] stu = new Student[3];
        stu[0] = new Student("zhangsan",21);
        stu[1] = new Student("lisi",19);
        stu[2] = new Student("wangwu",20);

        System.out.println("排序前：" + Arrays.toString(stu));
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(stu,nameComparator);
        System.out.println("排序后：" + Arrays.toString(stu));
    }
}
